<template>
  <div class="main-menu">
    <div class="logo-wrapper">
      <img class="logo" src="@/assets/img/png/logo.png" alt="logo" />
      <!--      <span v-show="!isFold" class="title">弘源管理系统</span>-->
      <span v-show="!isFold" class="title">Ethan CMS</span>
    </div>
    <el-menu
      :default-active="defaultActiveMenu"
      :collapse="isFold"
      text-color="#b7bdc3"
      active-text-color="#fff"
      background-color="#001529"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 1.系统总览 -->
        <el-sub-menu :index="String(item.id)">
          <template #title>
            <el-icon>
              <component :is="item.icon.split('el-icon-')[1]"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <template v-for="subItem in item.children" :key="subItem.id">
            <el-menu-item :index="subItem.id + ''" @click="handleItemClick(subItem.url)">
              {{ subItem.name }}
            </el-menu-item>
          </template>
        </el-sub-menu>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { useLoginStore } from '@/store/login';
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { mapPathToMenu } from '@/utils/map-menus';

const router = useRouter();

interface propsType {
  isFold: boolean;
}
withDefaults(defineProps<propsType>(), {
  isFold: false
});

const loginStore = useLoginStore();
const { userMenus } = loginStore;

const route = useRoute();

// 获取默认选中的菜单
const defaultActiveMenu = computed(() => {
  const menuInstance = mapPathToMenu(route.path, userMenus);
  return String(menuInstance?.id);
});

function handleItemClick(url: string) {
  router.push(url);
}
</script>

<style scoped lang="scss">
@import '@/assets/scss/mixin';

.main-menu {
  height: 100%;
  background-color: #001529;
  overflow: auto;
  //overflow-x: hidden;

  .logo-wrapper {
    @include flex(flex-start, center);
    margin: 12px 10px 8px 10px;
    overflow: hidden;

    .logo {
      height: 40px;
      margin-right: 10px;
      padding: 6px;
      border-radius: 50%;
      background-color: #ffffff;
    }

    .title {
      @include font(20px, 700, #fff);
      white-space: nowrap;
    }
  }

  .el-menu {
    border-right: none;
    user-select: none;
    margin-top: 24px;

    .el-sub-menu {
      //:deep(.el-sub-menu__title) {
      //  padding-left: 25px;
      //}
      .el-menu-item {
        padding-left: 50px !important;
        background-color: #0c2135;
      }

      .el-menu-item:hover {
        color: #fff;
      }

      .el-menu-item.is-active {
        background-color: #0a60bd;
      }
    }
  }
}
</style>
